﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApp.View.SystemInformation
{
    /// <summary>
    /// WD_InsertTraveerInformation.xaml 的交互逻辑
    /// </summary>
    public partial class WD_InsertTraveerInformation : Window
    {
        public WD_InsertTraveerInformation()
        {
            InitializeComponent();
        }
        BLL.UCTravellerInformation.UC_TravellerInformationClient myUCTravellerInformation = new BLL.UCTravellerInformation.UC_TravellerInformationClient();
        /// <summary>
        /// 页面加载时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            #region 证件类型下拉框绑定
            DataTable dsCertificateType = myUCTravellerInformation.SelectCertificateType().Tables[0];
            cbo_CertificateType.ItemsSource = dsCertificateType.DefaultView;
            cbo_CertificateType.SelectedValuePath = "detailedAttributeID";
            cbo_CertificateType.DisplayMemberPath = "CertificateType_name";
            #endregion
            #region 旅客类型下拉框的绑定
            DataTable dsPassengerType = myUCTravellerInformation.SelectPassengerType().Tables[0];
            cbo_TravellerType.ItemsSource = dsPassengerType.DefaultView;
            cbo_TravellerType.SelectedValuePath = "asPassengerID";
            cbo_TravellerType.DisplayMemberPath = "as_PassengerName";
            #endregion
            #region 性别下拉框的查询
            DataTable dsGenderType = myUCTravellerInformation.SelectGenderType().Tables[0];
            cbo_Gender.ItemsSource= dsGenderType.DefaultView;
            cbo_Gender.SelectedValuePath = "asGenderID";
            cbo_Gender.DisplayMemberPath = "as_GenderName";
            #endregion
        }
        /// <summary>
        /// 新增旅客信息（保存按钮）
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //判断页面数据是否为空
                if(txt_name.Text.Trim()!=string.Empty&&Convert.ToInt32(cbo_CertificateType.SelectedValue)>0&&Convert.ToInt32(cbo_TravellerType.SelectedValue)>0&& txt_CertificateType.Text.Trim()!=string.Empty
                    && Convert.ToInt32(cbo_Gender.SelectedValue) > 0 && txt_Age.Text.Trim() != string.Empty)
                {
                    //获取页面数据
                    //as_CertificateTypeID, as_PassengerTypeID, userName, as_GenderID, dateOfBirth, theAge, countryOrRegion, 
                    // certificateNumber, phoneNumber, emergencyPhone, userAddres, emergencyAddress, zipCode, registerDate
                    int as_CertificateTypeID = Convert.ToInt32(cbo_CertificateType.SelectedValue);
                    int as_PassengerTypeID = Convert.ToInt32(cbo_TravellerType.SelectedValue);
                    string userName = txt_name.Text.Trim();
                    int as_GenderID = Convert.ToInt32(cbo_Gender.SelectedValue);
                    DateTime dateOfBirth = Convert.ToDateTime(dtp_Birthday.Text.Trim());
                    string theAge = txt_Age.Text.Trim();
                    string countryOrRegion = txt_Contry.Text.Trim();
                    string certificateNumber = txt_CertificateType.Text.Trim();
                    string phoneNumber = txt_phone.Text.Trim();
                    string emergencyPhone = txt_emergency_phone.Text.Trim();
                    string userAddres = txt_address.Text.Trim();
                    string emergencyAddress = txt_emergency_address.Text.Trim();
                    string zipCode = txt_zip_code.Text.Trim();
                    DateTime registerDate = Convert.ToDateTime(dtp_RegisterDate.Text.ToString());
                    /*（3）新增保存
                     * -1 重复
                     *  1 成功
                     */
                    int intCount = myUCTravellerInformation.InsertPassengerInformation(as_CertificateTypeID,as_PassengerTypeID,userName,as_GenderID,dateOfBirth,theAge
                           ,countryOrRegion,certificateNumber,phoneNumber,emergencyPhone, userAddres,emergencyAddress,zipCode,registerDate);
                    if (intCount == -1)
                    {
                        MessageBox.Show("旅客重复！", "系统提示", MessageBoxButton.OKCancel,
                          MessageBoxImage.Exclamation); //弹出确定对话框
                    }
                    else
                    {
                        MessageBoxResult dr = MessageBox.Show("您新添加了一个旅客！", "系统提示", MessageBoxButton.OKCancel, MessageBoxImage.Asterisk); //弹出确定对话框                        
                        this.Close();
                    }

                }
                else
                {
                    MessageBox.Show("请把页面数据填写完整！", "系统提示", MessageBoxButton.OK,
                         MessageBoxImage.Warning); //弹出确定对话框
                }


            }
            catch (Exception)
            {
                return;
            }
        }
        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult dr = MessageBox.Show("退出页面数据将不保留。", "系统提示", MessageBoxButton.OKCancel, MessageBoxImage.Information);//弹出确定对话框
            if (dr == MessageBoxResult.OK)//如果点了确定按钮
            {
                //关闭窗口
                this.Close();
            }
        }

        private void txt_CertificateType_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void txt_phone_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void cbo_Gender_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }
    }
}
